package com.szy.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.SmartRequest;
import com.jspsmart.SmartUpload;
import com.jspsmart.SmartUploadException;
import com.szy.web.dao.ClientUserDao;
import com.szy.web.dao.CommentDAO;
import com.szy.web.model.Page;

public class ManagerClientUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		String method = req.getParameter("method");
//		if (method.equalsIgnoreCase("manager")) 
//		{
//			resp.sendRedirect("index.jsp");
//			String username = req.getParameter("username");
//			System.out.println("username = " + username);
//			resp.sendRedirect("managerClientUser.jsp");
//		}
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
//		 if ( request.getSession().getAttribute("Login") != null) 
//		   {
//			  
//				String message = (String) request.getSession().getAttribute("Login");
//				if (!message.equals("true"))
//				{
//					 //如果登录标志不为true则跳转回登录页面
//					 response.sendRedirect("index.jsp");
//					 return;
//				}
//				else
//				{
//					request.getSession().setAttribute("Login","true");
//				}   
//		   }
//		   else
//		   {
//		    	response.sendRedirect("index.jsp");
//		    	return;
//		   }
		String method = request.getParameter("method");
		if (method.equalsIgnoreCase("manager")) 
		{
			String username = request.getParameter("username");
			String page = request.getParameter("currentPage");
			int currentPage = 1;
			int pageSize = 10;
			if ( page != null )  
			{
				currentPage = Integer.parseInt(page);
				System.out.println("currentPage  = "+ currentPage);
			}
			try 
			{
				CommentDAO  commentDao = new CommentDAO(); 
				Page pageModel = commentDao.showComment(currentPage, pageSize,username );// 查询新闻从currPage到currPage+pageSize
				request.setAttribute("pageModel", pageModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getSession ( ).setAttribute ( "username", username );
				System.out.println("查看"+username+"OK");
				request.getRequestDispatcher("managerClientUser.jsp").forward(request,response);
			} catch (ClassNotFoundException e) 
			{
				
				e.printStackTrace();
			}
			
		}
		else if ( method.equalsIgnoreCase("slient")) 
		{
			String username = request.getParameter("username");
		
			
			System.out.println("username = "+username);	
			
			
			int currentPage = 1;
			int pageSize = 10;
//			String page = request.getParameter("currentPage");
//			System.out.println("currentPage = " + page);
//			if ( page != null )  
//			{
//				currentPage = Integer.parseInt(page);
//				System.out.println("currentPage  = "+ currentPage);
//			}
			 ServletConfig config = getServletConfig();
			 SmartUpload su= new SmartUpload();
			try 
			{
				su.initialize(config, request, response);
				su.setAllowedFilesList("gif,jpg,jpeg,png");
				su.setMaxFileSize(1024*1024*10);
				su.upload();
				SmartRequest req = su.getRequest();	
				int select = Integer.parseInt(req.getParameter("slientSelect"));
				System.out.println("select = "+ select);
				CommentDAO  commentDao = new CommentDAO(); 
				ClientUserDao clientUserDao;
				clientUserDao = new ClientUserDao();
			
				clientUserDao.setSendMsgSatus( username, select);
				Page pageModel = commentDao.showComment(currentPage, pageSize,username );// 查询新闻从currPage到currPage+pageSize
				request.setAttribute("pageModel", pageModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getSession ( ).setAttribute ( "username", username );
				System.out.println("查看"+username+"OK");
				request.getRequestDispatcher("managerClientUser.jsp").forward(request,response);
			} 
				catch (ClassNotFoundException e) 
			{
				
				e.printStackTrace();
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
			catch (SmartUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("修改成功");
		}
		
	}
}

